﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

//--Additional Libraries
using Microsoft.Phone.Notification;
using System.IO.IsolatedStorage;

using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Core;
using System.Device.Location;

using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;

using System.Windows.Navigation;

namespace findMePanorama
{
    public partial class fullMap : PhoneApplicationPage
    {
        // <-- Map Global-->
        MapLayer imageLayer;
        // <-- End Map Global -->

        // <-- Service/storage Global -->
        FindMeFriendServiceReference.FindMeFriendServiceClient serviceClient;
        IsolatedStorageSettings settings;
//<-- End Service/storage Global -->
        public fullMap()
        {
            InitializeComponent();
            //<-- Public Map Vars -->


            //Create a layer to contain the pushpin images.
            imageLayer = new MapLayer();
            map.Children.Add(imageLayer);
            //<--End Public Map Vars -->

        }

        //<-- Map Size for Orientation Landscape-->
        protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            base.OnOrientationChanged(e);

            if (e.Orientation == PageOrientation.PortraitUp)
            {
                NavigationService.Navigate(new Uri("/panorama.xaml", UriKind.Relative));
            }
        }
        // <-- Map END for Orientation -- > 

        ///<-- friends Map Configuration -->

        private GeoCoordinateWatcher _geo;
        public GeoPositionAccuracy Accuracy = GeoPositionAccuracy.High;
        private string _mode = "Road";


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            LocationInitialize();    
        }



        private void LocationInitialize()
        {




            // Reinitialize the GeoCoordinateWatcher
            _geo = new GeoCoordinateWatcher(Accuracy) { MovementThreshold = 20 };

            // Add event handlers for StatusChanged and PositionChanged events
            _geo.PositionChanged -= GeoPositionChanged;

            _geo.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(GeoPositionChanged);

            // Start data acquisition
            _geo.Start();
        }

        void GeoPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            var cor = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);

            //Using the current coordinates define the Map center to those coordinates.
            map.Center = cor;
            map.ZoomLevel = 15;

            /// <-- Start Image for center of Map -->

            // Create a pushpin to put at the center of the view.

            Pushpin pin1 = new Pushpin();

            pin1.Location = cor;
            //Clears other (pin1)    
            map.Children.Clear();
            //----------------
            map.Children.Add(pin1);


            // Use the Windows Phone pushpin style from App.xaml.
            pin1.Style = (Style)(Application.Current.Resources["PushpinStyle"]);
            pin1.PositionOrigin = PositionOrigin.BottomLeft;
            pin1.Content = "Me";

            ///<! --- Second Pin or friends pins

            Pushpin pin2 = new Pushpin();
            pin2.Location = new GeoCoordinate(18.24, -66.10);
            map.Children.Add(pin2);

            // Content of pin
            pin2.Content = "Hector Asencio";

            //Event for pin
            pin2.MouseLeftButtonUp += new MouseButtonEventHandler(pin2_MouseLeftButtonUp);
            /// <---End Second Pin-->


            /*
             // Define the image to use as the pushpin icon.
            Image pinImage = new Image();

            //Define the URI location of the image.
            pinImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("bluepushpin.png", UriKind.Relative));
            
            //Define the image display properties.
            pinImage.Opacity = 0.8;
            pinImage.Stretch = System.Windows.Media.Stretch.None;

            // Put the image at the center of the view.
            PositionOrigin position = PositionOrigin.Center;
            imageLayer.AddChild(pinImage, cor, position);
                */
            /// <-- End Image for center of Map -->

        }

       
        /// <summary>
        /// Pins Actions
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pin2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/contact.xaml", UriKind.Relative));
        }
        //<!-- End Pin Actions -->




        /// <summary>
        /// NavBar Menu Items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sync_Click(object sender, EventArgs e)
        {
            Accuracy = Accuracy == GeoPositionAccuracy.Default ? GeoPositionAccuracy.High : GeoPositionAccuracy.Default;
            LocationInitialize();
        }

        private void mapView_Click(object sender, EventArgs e)
        {
            ApplicationBarMenuItem btnMapView = (ApplicationBarMenuItem)ApplicationBar.MenuItems[0];

            if (_mode == "Road")
            {
                map.Mode = new AerialMode();
                _mode = "Aerial";
                btnMapView.Text = "change map to road view";
            }
            else
            {

                map.Mode = new RoadMode();
                _mode = "Road";
                btnMapView.Text = "change map to aerial view";

            }
        }
        /// End NavBar Menu Items

       
        private void about_Click(object sender, EventArgs e)
        {

        }

        private void pref_Click(object sender, EventArgs e)
        {

        }

        private void logout_Click(object sender, EventArgs e)
        {
            string username = settings["Username"].ToString();
            Guid sessionToken = (Guid)settings["SessionToken"];

            serviceClient.LogOffAsync(username, sessionToken);
            settings["SessionToken"] = new Guid();
            settings.Save();
            NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
        }

        ///<--End frieds Map Configuration -->

        

    }
}